basic.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <view class="container main">
  3. <view class="one">
  4. <form @submit="formSubmit">
  5. <view class="value other" style="display: none;">
  6. <view class="title">id</view>
  7. <view class="label">
  8. <input name="id" class="input" :value="form.id" placeholder="请输入id" />
  9. </view>
  10. </view>
  11. <view class="remark">
  12. 实名信息(仅用于身份验证,参加双创活动等)
  13. </view>
  14. <view class="value other">
  15. <view class="title">账号</view>
  16. <view class="label">
  17. <text v-if="form.account">{{form.account}}</text>
  18. <input v-else name="account" class="input" :value="form.account" placeholder="请输入账号" />
  19. </view>
  20. </view>
  21. <view class="value other margin">
  22. <view class="title">昵称</view>
  23. <view class="label">
  24. <input name="nick_name" class="input" :value="form.nick_name" placeholder="请输入昵称" />
  25. <span v-if="errors.nick_name" class="error-message">{{ errors.nick_name }}</span>
  26. </view>
  27. </view>
  28. <view class="value other">
  29. <view class="title">所属产业</view>
  30. <view class="label">
  31. <picker class="picker" mode="selector" :range="industryList" @change="industryChange"
  32. range-key="title">
  33. <view>{{form.industry||'请选择所属产业'}}</view>
  34. </picker>
  35. <text class="iconfont icon-dayuhao"></text>
  36. <span v-if="errors.industry" class="error-message">{{ errors.industry }}</span>
  37. </view>
  38. </view>
  39. <view class="value other">
  40. <view class="title">性别</view>
  41. <view class="label">
  42. <picker class="picker" mode="selector" :range="genderList" @change="genderChange"
  43. range-key="label">
  44. <view>{{gender_name||'请选择性别'}}</view>
  45. </picker>
  46. <text class="iconfont icon-dayuhao"></text>
  47. <span v-if="errors.gender" class="error-message">{{ errors.gender }}</span>
  48. </view>
  49. </view>
  50. <view class="value other">
  51. <view class="title">手机号</view>
  52. <view class="label">
  53. <input name="phone" class="input" :value="form.phone" placeholder="请输入手机号" />
  54. <span v-if="errors.phone" class="error-message">{{ errors.phone }}</span>
  55. </view>
  56. </view>
  57. <view class="value other">
  58. <view class="title">电子邮箱</view>
  59. <view class="label">
  60. <input name="email" class="input" :value="form.email" placeholder="请输入电子邮箱" />
  61. <span v-if="errors.email" class="error-message">{{ errors.email }}</span>
  62. </view>
  63. </view>
  64. <view class="button">
  65. <button type="warn" form-type="submit">保存</button>
  66. </view>
  67. </form>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. export default {
  73. components: {},
  74. data() {
  75. return {
  76. openid: '',
  77. user: {},
  78. form: {
  79. industry: '',
  80. },
  81. gender_name: '',
  82. // 字典表
  83. genderList: [],
  84. industryList: [],
  85. errors: {}
  86. }
  87. },
  88. onLoad: async function(e) {
  89. const that = this;
  90. await that.searchOpenids();
  91. await that.searchToken();
  92. await that.searchOther();
  93. await that.search();
  94. },
  95. methods: {
  96. async searchOpenids() {
  97. const that = this;
  98. uni.getStorage({
  99. key: 'openid',
  100. success: function(res) {
  101. that.$set(that, `openid`, res.data);
  102. },
  103. fail: function(err) {
  104. uni.login({
  105. success: async function(res) {
  106. if (res.code) {
  107. const aee = await that.$app('/wechat/api/login/app',
  108. 'GET', {
  109. js_code: res.code,
  110. config: that.$config.wx_projectkey
  111. })
  112. if (aee.errcode == '0') {
  113. uni.setStorage({
  114. key: "openid",
  115. data: aee.data.openid
  116. })
  117. that.$set(that, `openid`, aee.data.openid);
  118. } else {
  119. uni.showToast({
  120. title: aee.errmsg,
  121. icon: 'none'
  122. })
  123. }
  124. } else {
  125. uni.showToast({
  126. title: res.errMsg,
  127. icon: 'none'
  128. })
  129. }
  130. }
  131. });
  132. }
  133. })
  134. },
  135. // 用户信息
  136. searchToken() {
  137. const that = this;
  138. try {
  139. const res = uni.getStorageSync('token');
  140. if (res) {
  141. const user = that.$jwt(res);
  142. that.$set(that, `user`, user);
  143. }
  144. } catch (e) {}
  145. },
  146. async searchOther() {
  147. const that = this;
  148. let res;
  149. // 查询性别
  150. res = await that.$api(`/dictData`, 'GET', {
  151. code: 'gender',
  152. is_use: '0',
  153. })
  154. if (res.errcode == '0') that.$set(that, `genderList`, res.data);
  155. // 查询所属产业
  156. res = await that.$api(`/sector`, 'GET', {
  157. is_use: '0',
  158. })
  159. if (res.errcode == '0') that.$set(that, `industryList`, res.data);
  160. },
  161. // 查询
  162. async search() {
  163. const that = this;
  164. if (that.user && that.user.id) {
  165. let res;
  166. res = await that.$api(`/user/${that.user.id}`, 'GET', {})
  167. if (res.errcode == '0') {
  168. that.$set(that, `form`, res.data)
  169. if (res.data.gender) {
  170. let data = that.genderList.find(i => i.value == res.data.gender);
  171. if (data) that.$set(that, `gender_name`, data.label)
  172. }
  173. } else {
  174. uni.showToast({
  175. title: res.errmsg,
  176. });
  177. }
  178. }
  179. },
  180. // 性别选择
  181. genderChange(e) {
  182. const that = this;
  183. let data = that.genderList.find(i => i.value == e.detail.value);
  184. if (data) {
  185. that.$set(that.form, `gender`, data.value)
  186. that.$set(that, `gender_name`, data.label)
  187. }
  188. },
  189. // 产业选择
  190. industryChange(e) {
  191. const that = this;
  192. let data = that.industryList[e.detail.value];
  193. if (data) {
  194. that.$set(that.form, `industry`, data.title)
  195. }
  196. },
  197. // 自定义的验证函数
  198. validateObject(obj) {
  199. const errors = {};
  200. if (!obj.nick_name || obj.nick_name.trim() === '') {
  201. errors.nick_name = '请填写昵称!';
  202. }
  203. if (!obj.industry || obj.industry.trim() === '') {
  204. errors.industry = '请选择所属产业!';
  205. }
  206. if (!obj.gender || obj.gender.trim() === '') {
  207. errors.gender = '请选择性别!';
  208. }
  209. if (!obj.phone || obj.phone.trim() === '') {
  210. errors.phone = '请填写手机号!';
  211. } else {
  212. const regex = /^1[3456789]\d{9}$/;
  213. if (!regex.test(obj.phone)) errors.phone = '请填写正确的手机号码!';
  214. }
  215. if (!obj.email || obj.email.trim() === '') {
  216. errors.email = '请填写电子邮箱!';
  217. } else {
  218. const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
  219. if (!regex.test(obj.email)) errors.email = '请填写正确的电子邮箱!';
  220. }
  221. // 如果有错误,返回错误对象
  222. if (Object.keys(errors).length > 0) {
  223. return errors;
  224. }
  225. // 如果没有错误,返回null或undefined
  226. return null;
  227. },
  228. async formSubmit(e) {
  229. const that = this;
  230. const form = that.form
  231. const data = e.detail.value;
  232. if (!form.openid) data.openid = that.openid
  233. data.gender = form.gender
  234. data.industry = form.industry
  235. const errorsInfo = await that.validateObject(data);
  236. // 检查是否有错误
  237. if (errorsInfo) {
  238. that.$set(that, `errors`, errorsInfo)
  239. // 遍历错误对象并显示错误信息
  240. for (const key in errorsInfo) {
  241. if (errorsInfo.hasOwnProperty(key)) {
  242. console.error(`${key} 错误: ${errorsInfo[key]}`);
  243. }
  244. }
  245. } else {
  246. that.$set(that, `errors`, {})
  247. const res = await that.$api(`/user/${that.user.id}`, 'POST', data);
  248. if (res.errcode == '0') that.search();
  249. else {
  250. uni.showToast({
  251. title: res.errmsg,
  252. });
  253. }
  254. }
  255. },
  256. }
  257. }
  258. </script>
  259. <style lang="scss" scoped>
  260. .main {
  261. background-color: var(--footColor);
  262. .one {
  263. .icon {
  264. padding: 2vw;
  265. }
  266. .margin {
  267. margin: 3vw 0 0 0;
  268. }
  269. .other {
  270. padding: 3vw 2vw;
  271. border-bottom: 1px solid var(--footColor);
  272. }
  273. .value {
  274. display: flex;
  275. justify-content: space-between;
  276. align-items: center;
  277. background-color: var(--mainColor);
  278. .label {
  279. text-align: right;
  280. .error-message {
  281. margin: 5px 0 0 0;
  282. color: var(--fF0Color);
  283. font-size: var(--font12Size);
  284. }
  285. }
  286. }
  287. .remark {
  288. padding: 4vw 2vw;
  289. text-align: center;
  290. color: var(--f99Color);
  291. font-size: var(--font18Size);
  292. }
  293. .button {
  294. margin: 2vw 0 0 0;
  295. padding: 2vw;
  296. text-align: center;
  297. button {
  298. background-color: var(--f3CColor);
  299. font-size: var(--font14Size);
  300. border-radius: 2vw;
  301. }
  302. }
  303. }
  304. }
  305. </style>