index.vue 7.3 KB

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