index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <view class="auth">
  3. <image src="/static/logo.jpg" mode="aspectFill"></image>
  4. <text class="margin-top-3 auth-title">欢迎使用爱企通小程序</text>
  5. <text class="margin-top-3 auth-content">此页面是微信授权页面,授权之后你可以获取更优质的服务,您的隐私将会受到保护</text>
  6. <view class="margin-top-3">
  7. <button type='default' class="refuse" @click="refuseBtn">暂不授权</button>
  8. <button type='primary' class="allow" @click="getUserInfo()" v-if="!isAuth">登录授权</button>
  9. <button type="primary" class="allow" open-type="getPhoneNumber" style="font-size: 12px;"
  10. @getphonenumber="getPhoneNumber" v-if="isAuth && !isNeedPhone">手机号码授权</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import md5 from '@/common/md5.js';
  16. export default {
  17. data(){
  18. return {
  19. iv:'',
  20. encryptedData:'',
  21. isNeedPhone:getApp().globalData.user_phone,
  22. isAuth:getApp().globalData.isAuth
  23. }
  24. },
  25. onLoad() {
  26. this.getUserInfoData()
  27. },
  28. methods:{
  29. getUserInfoData(){
  30. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  31. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&action=info_by_openid&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  32. uni.request({
  33. url:url,
  34. method: 'POST',
  35. header: {
  36. 'content-type': 'application/x-www-form-urlencoded'
  37. },
  38. data: {
  39. openId:getApp().globalData.open_id
  40. },
  41. success: (res) => {
  42. if(res.data.code === 200){
  43. if(res.data.data.nickname){
  44. getApp().globalData.isAuth = true;
  45. getApp().globalData.globalAuth = true;
  46. getApp().globalData.user_phone = false;
  47. getApp().globalData.user_name = res.data.data.nickname;
  48. this.isAuth = true;
  49. this.isNeedPhone = false;
  50. }else{
  51. getApp().globalData.user_name = '';
  52. getApp().globalData.isAuth = false;
  53. getApp().globalData.globalAuth = false;
  54. this.isAuth = false;
  55. }
  56. }
  57. },
  58. fail: () => {
  59. console.log("连接失败");
  60. }
  61. });
  62. },
  63. getUserInfo() {
  64. uni.getUserProfile({
  65. desc:'登录',
  66. success:(res)=> {
  67. getApp().globalData.user_headUrl = res.userInfo.avatarUrl;
  68. getApp().globalData.user_name = res.userInfo.nickName;
  69. this.iv = res.iv;
  70. this.encryptedData = res.encryptedData;
  71. this.isAuth = true;
  72. getApp().globalData.isAuth = true;
  73. getApp().globalData.globalAuth = true;
  74. this.loginUserInfo()
  75. },
  76. fail:(err)=> {
  77. getApp().globalData.isAuth = false;
  78. getApp().globalData.globalAuth = false;
  79. this.isAuth = false;
  80. }
  81. })
  82. // if (e.detail.errMsg == "getUserInfo:ok") {
  83. // getApp().globalData.user_headUrl = e.detail.userInfo.avatarUrl;
  84. // getApp().globalData.user_name = e.detail.userInfo.nickName;
  85. // this.iv = e.detail.iv;
  86. // this.encryptedData = e.detail.encryptedData;
  87. // console.log(e.detail)
  88. // //this.loginUserInfo()
  89. // } else {
  90. // console.log("用户信息授权失败");
  91. // getApp().globalData.isAuth = false;
  92. // }
  93. },
  94. getPhoneNumber(e){
  95. let that = this;
  96. if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
  97. console.log(e.detail)
  98. that.phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key)
  99. } else { //
  100. that.isNeedPhone = false;
  101. }
  102. },
  103. phoneRequest(myIv,myEncryptedData,sKey){
  104. let that = this;
  105. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  106. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&source=user&action=phone&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  107. uni.request({
  108. url:url,
  109. method: 'POST',
  110. header: {
  111. 'content-type': 'application/x-www-form-urlencoded'
  112. },
  113. data: {
  114. iv:myIv,
  115. sessionKey:sKey,
  116. encryptedData:myEncryptedData,
  117. openId:getApp().globalData.open_id
  118. },
  119. success: (res) => {
  120. console.log(res)
  121. if(res.data.code === 200){
  122. getApp().globalData.user_phone = res.data.data;
  123. setTimeout(()=>{
  124. that.refuseBtn();
  125. },300)
  126. }
  127. },
  128. fail: () => {
  129. console.log("连接失败");
  130. }
  131. });
  132. },
  133. refuseBtn(){
  134. uni.navigateBack({
  135. });
  136. },
  137. loginUserInfo(){
  138. let that = this;
  139. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  140. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&source=user&action=update&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  141. uni.request({
  142. url:url, //需要设置为全局
  143. method: 'POST',
  144. header: {
  145. 'content-type': 'application/x-www-form-urlencoded'
  146. },
  147. data: {
  148. openId:getApp().globalData.open_id,
  149. nickname:getApp().globalData.user_name,
  150. headimg:getApp().globalData.user_headUrl,
  151. },
  152. success: res => {
  153. if(res.data.code === 200){
  154. console.log(res.data)
  155. // getApp().globalData.user_id = res.data.msg.id;
  156. // getApp().globalData.open_id = res.data.msg.openid;
  157. // getApp().globalData.isAuth = true;
  158. // getApp().globalData.user_headUrl = res.data.msg.headimg;
  159. // getApp().globalData.user_name = res.data.msg.name;
  160. // getApp().globalData.user_status = res.data.msg.status;
  161. // getApp().globalData.user_phone = res.data.msg.phone;
  162. // that.isNeedPhone = res.data.msg.phone;
  163. // that.isAuth = getApp().globalData.isAuth;
  164. }
  165. else {
  166. uni.showToast({
  167. title: res.data.msg,
  168. icon: 'none',
  169. duration:2000
  170. });
  171. }
  172. // uni.navigateBack({
  173. // });
  174. }
  175. });
  176. },
  177. }
  178. }
  179. </script>
  180. <style>
  181. .auth{
  182. margin-top: 0;
  183. text-align: center;
  184. display: flex;
  185. flex-direction: column;
  186. justify-content: center;
  187. align-items: center;
  188. padding: 100upx;
  189. vertical-align: middle;
  190. }
  191. .auth image{
  192. width: 200upx;
  193. height: 200upx;
  194. }
  195. .auth view {
  196. display: flex;
  197. width: 490upx;
  198. justify-content: space-between;
  199. }
  200. .auth-title {
  201. font-size: 32upx;
  202. }
  203. .auth-content {
  204. font-size: 26upx;
  205. color: #a7aaa9;
  206. }
  207. .allow {
  208. background-color: #27BCEF;
  209. margin: 20rpx 0 200rpx;
  210. text-align: center;
  211. vertical-align: middle;
  212. touch-action: manipulation;
  213. cursor: pointer;
  214. background-image: none;
  215. white-space: nowrap;
  216. user-select: none;
  217. font-size: 14px;
  218. border: 0 !important;
  219. position: relative;
  220. text-decoration: none;
  221. height: 44px;
  222. width: 250rpx;
  223. line-height: 44px;
  224. box-shadow: inset 0 0 0 1px #27BCEF;
  225. background: #fff !important;
  226. color: #27BCEF !important;
  227. display: inline-block;
  228. border-radius: 10rpx;
  229. }
  230. .refuse {
  231. background-color: #19be6b;
  232. margin: 20rpx 20rpx 200rpx 20rpx;
  233. text-align: center;
  234. vertical-align: middle;
  235. touch-action: manipulation;
  236. cursor: pointer;
  237. background-image: none;
  238. white-space: nowrap;
  239. user-select: none;
  240. font-size: 14px;
  241. border: 0 !important;
  242. position: relative;
  243. text-decoration: none;
  244. height: 44px;
  245. width: 250rpx;
  246. line-height: 44px;
  247. box-shadow: inset 0 0 0 1px #8a8a8a;
  248. background: #fff !important;
  249. color: #8a8a8a !important;
  250. display: inline-block;
  251. border-radius: 10rpx;
  252. }
  253. .margin-top-3{
  254. margin-top: 10%;
  255. }
  256. </style>