图书馆小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
8.4 KiB

1 month ago
  1. <template>
  2. <view style="background-color: #fff; height: calc(100vh);">
  3. <view class="user-info-section">
  4. <!-- 点击获取头像 -->
  5. <button open-type="chooseAvatar" @chooseavatar="onChooseAvatar" class="avatar-btn">
  6. <image v-if="avatarUrl" :src="avatarUrl" class="avatar-img"></image>
  7. <image v-else src="@/static/images/avatar.png" class="avatar-img"></image>
  8. <text class="tip-text">点击选择头像</text>
  9. </button>
  10. </view>
  11. <view class="form-box">
  12. <view class="item">
  13. <uni-icons class="form-icon" type="person" size="24"></uni-icons>
  14. <view class="uni-input-wrapper">
  15. <input
  16. class="input"
  17. type="nickname"
  18. v-model="nickName"
  19. placeholder="请输入昵称"
  20. @input="handleInput('nickName')"
  21. />
  22. <uni-icons
  23. class="clear-icon"
  24. v-if="clearIconStatus.nickName"
  25. @click="clearInput('nickName')"
  26. type="close"
  27. size="20"
  28. ></uni-icons>
  29. </view>
  30. </view>
  31. <view class="item">
  32. <uni-icons class="form-icon" custom-prefix="iconfont" type="icon-duzhezheng" size="24"></uni-icons>
  33. <view class="uni-input-wrapper">
  34. <input
  35. class="input"
  36. placeholder="请输入读者证号"
  37. v-model="queryvalue"
  38. @input="handleInput('queryvalue')"
  39. />
  40. <uni-icons
  41. class="clear-icon"
  42. v-if="clearIconStatus.queryvalue"
  43. @click="clearInput('queryvalue')"
  44. type="close"
  45. size="20"
  46. ></uni-icons>
  47. </view>
  48. </view>
  49. <view class="item">
  50. <uni-icons class="form-icon" type="locked" size="24"></uni-icons>
  51. <input class="input" placeholder="请输入密码" :password="!showPwd" v-model="rdpasswd" />
  52. <uni-icons class="form-right-icon" :type="showPwd ? 'eye-slash' : 'eye'" size="20"
  53. @click="togglePwd"></uni-icons>
  54. </view>
  55. <button class="login-btn" type="primary" @click="submit">绑定</button>
  56. </view>
  57. <view class="tips">
  58. 温馨提示<br />
  59. 1密码默认为 <text style="color:#e74c3c">身份证后6位</text>如果身份证号最后一位为XX需要大写;
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import { FetchInitScreenSetting, FetchReaderList } from '@/api/user';
  65. const TOKEN_KEY = 'token';
  66. const USER_KEY = 'user-info';
  67. export default {
  68. data() {
  69. return {
  70. queryvalue: '',
  71. rdpasswd: '',
  72. showPwd: false,
  73. screenConfig: {},
  74. avatarUrl: '',
  75. nickName: '',
  76. // 清空按钮状态(支持多输入框)
  77. clearIconStatus: {
  78. nickName: false,
  79. queryvalue: false
  80. }
  81. };
  82. },
  83. onLoad() {
  84. this.getScreenSetting();
  85. },
  86. methods: {
  87. // 获取微信头像
  88. onChooseAvatar(e) {
  89. console.log('获取微信头像',e)
  90. this.avatarUrl = e.detail.avatarUrl;
  91. },
  92. // 统一监听输入(自动控制清空按钮显示隐藏)
  93. handleInput(field) {
  94. this.clearIconStatus[field] = this[field].trim().length > 0
  95. },
  96. // 统一清空输入框内容
  97. clearInput(field) {
  98. this[field] = ''
  99. this.clearIconStatus[field] = false
  100. },
  101. // 切换密码显隐
  102. togglePwd() {
  103. this.showPwd = !this.showPwd;
  104. },
  105. async getScreenSetting() {
  106. try {
  107. const res = await FetchInitScreenSetting({ libcode: '1201' });
  108. const data = res.data;
  109. this.screenConfig = {
  110. thirdUrl: data.open_lib_http?.context || '',
  111. thirdAppid: data.open_lib_appId?.context || '',
  112. thirdSecret: data.open_lib_secret?.context || '',
  113. sm4Key: data.sm4_key?.context || ''
  114. };
  115. } catch (err) {
  116. console.error('获取配置失败:', err);
  117. }
  118. },
  119. async submit() {
  120. // 去除首尾空格
  121. this.nickName = this.nickName.trim()
  122. this.queryvalue = this.queryvalue.trim()
  123. if (!this.queryvalue || !this.rdpasswd) {
  124. uni.showToast({ title: '请输入读者证号和密码', icon: 'none' });
  125. return;
  126. }
  127. uni.showLoading({ title: '绑定中...' });
  128. try {
  129. const params = {
  130. ...this.screenConfig,
  131. selecttype: 'rdid',
  132. queryvalue: this.queryvalue,
  133. rdpasswd: this.rdpasswd,
  134. };
  135. // {"code":200,"message":"操作成功","data":"{\"messagelist\":[{\"code\":\"R00138\",\"message\":\"未找到符合条件的读者!\"}],\"success\":false}","timestamp":1778154499544}
  136. // {"code":200,"message":"操作成功","data":"{\"success\":true,\"pagedata\":[{\"rdClusterCode\":null,\"rdlib\":\"GD\",\"rdid\":\"420105198509200438\",\"rdcfstate\":1}]}","timestamp":1778154647854}
  137. // {"code":200,"message":"操作成功","data":"{\"messageList\":[{\"R00131\":\"认证失败,系统存在该读者,密码不匹配!\"}],\"success\":true,\"pagedata\":\"\"}","timestamp":1778155520501}
  138. const res = await FetchReaderList(params);
  139. let result = {};
  140. try {
  141. result = JSON.parse(res.data);
  142. } catch (e) {
  143. uni.hideLoading();
  144. uni.showToast({ title: '数据解析失败', icon: 'none' });
  145. return;
  146. }
  147. // 统一提取错误信息
  148. let errMsg = '';
  149. if (result.messagelist?.length) {
  150. const item = result.messagelist[0];
  151. errMsg = item.message || Object.values(item)[0] || '认证失败';
  152. }
  153. if (result.messageList?.length) {
  154. const item = result.messageList[0];
  155. errMsg = item.message || Object.values(item)[0] || '认证失败';
  156. }
  157. // 成功判断
  158. const realSuccess = result.success === true && result.pagedata?.length > 0;
  159. if (realSuccess) {
  160. const loginRes = {
  161. token: 'reader-token-' + Date.now(),
  162. user: {
  163. nickName: this.nickName || '小图',
  164. avatarUrl: this.avatarUrl,
  165. cardNo: this.queryvalue
  166. }
  167. };
  168. // uni.setStorageSync(TOKEN_KEY, loginRes.token);
  169. // uni.setStorageSync(USER_KEY, loginRes.user);
  170. // uni.hideLoading();
  171. // uni.showToast({ title: '认证成功', icon: 'success' });
  172. const openId = uni.getStorageSync("wx_login_code");
  173. const bindParams = {
  174. ...this.screenConfig,
  175. openid: openId,
  176. bindValue: this.queryvalue,
  177. bindType: 'rdid',
  178. libcode: '1201',
  179. }
  180. const bindRes = await FetchBindReadCard(params);
  181. setTimeout(() => {
  182. uni.navigateBack();
  183. }, 1500);
  184. } else {
  185. uni.hideLoading();
  186. uni.showToast({
  187. title: errMsg || '读者证或密码错误',
  188. icon: 'none'
  189. });
  190. }
  191. } catch (err) {
  192. uni.hideLoading();
  193. uni.showToast({ title: '网络异常', icon: 'none' });
  194. }
  195. }
  196. }
  197. };
  198. </script>
  199. <style lang="scss" scoped>
  200. .user-info-section {
  201. display: flex;
  202. flex-direction: column;
  203. align-items: center;
  204. padding: 30px 0 0 0;
  205. .avatar-btn {
  206. background: transparent;
  207. display: flex;
  208. flex-direction: column;
  209. align-items: center;
  210. &::after{
  211. border: none !important;
  212. }
  213. .avatar-img {
  214. width: 60px;
  215. height: 60px;
  216. border-radius: 50%;
  217. }
  218. .tip-text {
  219. font-size: 12px;
  220. color: #999;
  221. }
  222. }
  223. }
  224. .form-box {
  225. padding: 20px 15px;
  226. .item {
  227. width: 100%;
  228. min-height: 44px;
  229. border-radius: 22px;
  230. background-color: #f7f7f7;
  231. display: flex;
  232. align-items: center;
  233. margin-bottom: 15px;
  234. .input {
  235. flex: 1;
  236. padding: 10px;
  237. font-size: 14px;
  238. }
  239. }
  240. .uni-input-wrapper{
  241. flex: 1;
  242. display: flex;
  243. align-items: center;
  244. .input {
  245. flex: 1;
  246. padding: 10px;
  247. font-size: 14px;
  248. }
  249. .clear-icon{
  250. padding: 0 12px;
  251. color: #ccc;
  252. }
  253. }
  254. }
  255. .login-btn {
  256. margin-top: 10px;
  257. background-color: #01a4fe !important;
  258. border-radius: 22px;
  259. font-size: 16px;
  260. height: 44px;
  261. line-height: 44px;
  262. }
  263. .tips {
  264. margin: 30px 20px;
  265. font-size: 12px;
  266. color: #333;
  267. line-height: 20px;
  268. }
  269. .form-icon {
  270. ::v-deep .uni-icons {
  271. margin-left: 10px;
  272. color: #01a4fe !important;
  273. }
  274. }
  275. .form-right-icon {
  276. ::v-deep .uni-icons {
  277. margin-right: 10px;
  278. }
  279. }
  280. </style>