index.vue 7.0 KB

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