index.vue 7.3 KB

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