설명 없음
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.

Users.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\user\model;
  14. use think\Db;
  15. use think\Model;
  16. use think\Config;
  17. /**
  18. * 会员
  19. */
  20. class Users extends Model
  21. {
  22. private $home_lang = 'cn';
  23. private $appid = '';
  24. private $mchid = '';
  25. private $key = '';
  26. //初始化
  27. protected function initialize()
  28. {
  29. // 需要调用`Model`的`initialize`方法
  30. parent::initialize();
  31. $this->home_lang = get_home_lang();
  32. }
  33. // 判断会员属性中必填项是否为空
  34. // 传入参数:
  35. // $post_users :会员属性信息数组
  36. // return error:错误提示
  37. public function isEmpty($post_users = [], $type = '', $return = 'string')
  38. {
  39. $error = '';
  40. // 会员属性
  41. $where = array(
  42. 'lang' => $this->home_lang,
  43. 'is_hidden' => 0, // 是否隐藏属性,0为否
  44. 'is_required' => 1, // 是否必填属性,1为是
  45. );
  46. if ('reg' == $type) {
  47. $where['is_reg'] = 1; // 是否为注册表单
  48. }
  49. $para_data = M('users_parameter')->where($where)->field('title,name')->select();
  50. // 处理提交的属性中必填项是否为空
  51. foreach ($para_data as $key => $value) {
  52. if (isset($post_users[$value['name']])) {
  53. if (is_array($post_users[$value['name']])) {
  54. $post_users[$value['name']] = implode(',', $post_users[$value['name']]);
  55. }
  56. $attr_value = trim($post_users[$value['name']]);
  57. if (empty($attr_value)) {
  58. if ('array' === $return) {
  59. return [
  60. 'field' => $value['name'],
  61. 'msg' => $value['title'].'不能为空!',
  62. ];
  63. } else {
  64. return $value['title'].'不能为空!';
  65. }
  66. }
  67. }
  68. }
  69. return false;
  70. }
  71. // 判断邮箱和手机是否存在,并且判断验证码是否验证通过
  72. // 传入参数:
  73. // $post_users:会员属性信息数组
  74. // $users_id:会员ID,注册时不需要传入,修改时需要传入。
  75. // return error
  76. public function isRequired($post_users = [],$users_id='', $type = '', $return = 'string')
  77. {
  78. if (empty($post_users)) {
  79. return false;
  80. }
  81. // 处理邮箱和手机是否存在
  82. $where_1 = [
  83. 'is_system'=> 1,
  84. 'lang' => $this->home_lang,
  85. ];
  86. $where_1[] = Db::raw(" ( name LIKE 'email_%' OR name LIKE 'mobile_%' ) ");
  87. if ('reg' == $type) {
  88. $where_1['is_reg'] = 1; // 是否为注册表单
  89. }
  90. $users_parameter = M('users_parameter')->where($where_1)->field('para_id,title,name')->getAllWithIndex('name');
  91. $email = '';
  92. $email_code = '';
  93. $mobile = '';
  94. $mobile_code = '';
  95. /*获取邮箱和手机号码*/
  96. foreach ($post_users as $key => $val) {
  97. if (preg_match('/^email_/i', $key)) {
  98. if (!preg_match('/(_code|_vertify)$/i', $key)) {
  99. $email = $val;
  100. if (!empty($val) && !check_email($val)) {
  101. if ('array' == $return) {
  102. return [
  103. 'code_status' => 0,
  104. 'field' => $key,
  105. 'msg' => $users_parameter[$key]['title'].'格式不正确!',
  106. ];
  107. } else {
  108. return $users_parameter[$key]['title'].'格式不正确!';
  109. }
  110. }
  111. } else {
  112. $email_code = $val;
  113. }
  114. } else if (preg_match('/^mobile_/i', $key)) {
  115. if (!preg_match('/(_code|_vertify)$/i', $key)) {
  116. $mobile = $val;
  117. if (!empty($val) && !check_mobile($val)) {
  118. if ('array' == $return) {
  119. return [
  120. 'code_status' => 0,
  121. 'field' => $key,
  122. 'msg' => $users_parameter[$key]['title'].'格式不正确!',
  123. ];
  124. } else {
  125. return $users_parameter[$key]['title'].'格式不正确!';
  126. }
  127. }
  128. } else {
  129. $mobile_code = $val;
  130. }
  131. }
  132. }
  133. /*--end*/
  134. $users_verification = getUsersConfigData('users.users_verification');
  135. if (2 == $users_verification) {
  136. $time = getTime();
  137. /*处理邮箱验证码逻辑*/
  138. if (!empty($email)) {
  139. $where = [
  140. 'email' => $email,
  141. 'code' => $email_code,
  142. 'lang' => $this->home_lang,
  143. ];
  144. !empty($users_id) && $where['users_id'] = $users_id;
  145. $record = M('smtp_record')->where($where)->field('record_id,status,add_time')->find();
  146. if (!empty($record)) {
  147. $record['add_time'] += Config::get('global.email_default_time_out');
  148. if (1 == $record['status'] || $record['add_time'] <= $time) {
  149. if ('array' == $return) {
  150. return [
  151. 'code_status' => 0,
  152. 'field' => 'email_1_code',
  153. 'msg' => '邮箱验证码已被使用或超时,请重新发送!',
  154. ];
  155. } else {
  156. return '邮箱验证码已被使用或超时,请重新发送!';
  157. }
  158. }else{
  159. // 返回后处理邮箱验证码失效操作
  160. $data = [
  161. 'code_status' => 1,// 正确
  162. 'email' => $email,
  163. ];
  164. return $data;
  165. }
  166. }else{
  167. if (!empty($users_id)) {
  168. // 当会员修改邮箱地址,验证码为空或错误返回
  169. $row = $this->getUsersListData('email',$users_id);
  170. if ($email != $row['email']) {
  171. if ('array' == $return) {
  172. return [
  173. 'code_status' => 0,
  174. 'field' => 'email_1_code',
  175. 'msg' => '邮箱验证码不正确,请重新输入!',
  176. ];
  177. } else {
  178. return '邮箱验证码不正确,请重新输入!';
  179. }
  180. }
  181. }else{
  182. // 当会员注册时,验证码为空或错误返回
  183. if ('array' == $return) {
  184. return [
  185. 'code_status' => 0,
  186. 'field' => 'email_1_code',
  187. 'msg' => '邮箱验证码不正确,请重新输入!',
  188. ];
  189. } else {
  190. return '邮箱验证码不正确,请重新输入!';
  191. }
  192. }
  193. }
  194. }
  195. /*--end*/
  196. } else if (3 == $users_verification) {
  197. $time = getTime();
  198. /*处理短信验证码逻辑*/
  199. if (!empty($mobile)) {
  200. $where = [
  201. 'mobile' => $mobile,
  202. 'code' => $mobile_code
  203. ];
  204. $smslog = Db::name('sms_log')->where($where)->field('is_use, add_time')->order('id desc')->find();
  205. if (!empty($smslog)) {
  206. $smslog['add_time'] += Config::get('global.mobile_default_time_out');
  207. if (1 == $smslog['is_use'] || $smslog['add_time'] <= $time) {
  208. if ('array' == $return) {
  209. $data = [
  210. 'code_status' => 0,
  211. 'field' => 'mobile_1_code',
  212. 'msg' => '短信验证码不正确,请重新输入!',
  213. ];
  214. } else {
  215. $data = '短信验证码不正确,请重新输入!';
  216. }
  217. } else {
  218. // 返回后处理短信验证码失效操作
  219. $data = [
  220. 'code_status' => 1,// 正确
  221. 'mobile' => $mobile
  222. ];
  223. }
  224. } else {
  225. if (!empty($users_id)) {
  226. // 当会员修改手机地址,验证码为空或错误返回
  227. $row = $this->getUsersListData('mobile', $users_id);
  228. if ($mobile != $row['mobile']) {
  229. if ('array' == $return) {
  230. $data = [
  231. 'code_status' => 0,
  232. 'field' => 'mobile_1_code',
  233. 'msg' => '短信验证码不正确,请重新输入!',
  234. ];
  235. } else {
  236. $data = '短信验证码不正确,请重新输入!';
  237. }
  238. }
  239. } else {
  240. // 当会员注册时,验证码为空或错误返回
  241. if ('array' == $return) {
  242. $data = [
  243. 'code_status' => 0,
  244. 'field' => 'mobile_1_code',
  245. 'msg' => '短信验证码不正确,请重新输入!',
  246. ];
  247. } else {
  248. $data = '短信验证码不正确,请重新输入!';
  249. }
  250. }
  251. }
  252. return $data;
  253. }
  254. /*--end*/
  255. }
  256. foreach ($users_parameter as $key => $value) {
  257. if (isset($post_users[$value['name']])) {
  258. $where_2 = [
  259. 'para_id' => ['EQ', $value['para_id']],
  260. 'info' => trim($post_users[$value['name']]),
  261. 'users_id' => ['NEQ', $users_id],
  262. 'lang' => $this->home_lang,
  263. ];
  264. // 若users_id为空,则清除条件中的users_id条件
  265. if (empty($users_id)) { unset($where_2['users_id']); }
  266. $users_list = M('users_list')->where($where_2)->field('info')->find();
  267. if (!empty($users_list['info'])) {
  268. if ('array' == $return) {
  269. return [
  270. 'code_status' => 0,
  271. 'field' => $key,
  272. 'msg' => $value['title'].'已存在!',
  273. ];
  274. } else {
  275. return $value['title'].'已存在!';
  276. }
  277. }
  278. }
  279. }
  280. return false;
  281. }
  282. // 查询会员属性信息表的邮箱和手机字段
  283. // 必须传入参数:
  284. // users_id 会员ID
  285. // field 查询字段,email仅邮箱,mobile仅手机号,*为两项都查询。
  286. // return Data
  287. public function getUsersListData($field,$users_id)
  288. {
  289. $Data = array();
  290. if ('email' == $field || '*' == $field) {
  291. // 查询邮箱
  292. $parawhere = [
  293. 'name' => ['LIKE', "email_%"],
  294. 'is_system' => 1,
  295. 'lang' => $this->home_lang,
  296. ];
  297. $paraData = M('users_parameter')->where($parawhere)->field('para_id')->find();
  298. $listwhere = [
  299. 'para_id' => $paraData['para_id'],
  300. 'users_id' => $users_id,
  301. 'lang' => $this->home_lang,
  302. ];
  303. $listData = M('users_list')->where($listwhere)->field('users_id,info')->find();
  304. $Data['email'] = !empty($listData['info']) ? $listData['info'] : '';
  305. }
  306. if ('mobile' == $field || '*' == $field) {
  307. // 查询手机号
  308. $parawhere_1 = [
  309. 'name' => ['LIKE', "mobile_%"],
  310. 'is_system' => 1,
  311. 'lang' => $this->home_lang,
  312. ];
  313. $paraData_1 = M('users_parameter')->where($parawhere_1)->field('para_id')->find();
  314. $listwhere_1 = [
  315. 'para_id' => $paraData_1['para_id'],
  316. 'users_id' => $users_id,
  317. 'lang' => $this->home_lang,
  318. ];
  319. $listData_1 = M('users_list')->where($listwhere_1)->field('users_id,info')->find();
  320. $Data['mobile'] = !empty($listData_1['info']) ? $listData_1['info'] : '';
  321. }
  322. return $Data;
  323. }
  324. /**
  325. * 查询解析数据表的数据用以构造from表单
  326. * @param return $list
  327. * @param 用于添加,不携带数据
  328. * @author 陈风任 by 2019-2-20
  329. */
  330. public function getDataPara($source = '')
  331. {
  332. // 字段及内容数据处理
  333. $where = array(
  334. 'lang' => $this->home_lang,
  335. 'is_hidden' => 0,
  336. );
  337. 'reg' == $source && $where['is_reg'] = 1;
  338. $row = M('users_parameter')->field('*')
  339. ->where($where)
  340. ->order('sort_order asc,para_id asc')
  341. ->select();
  342. // 根据所需数据格式,拆分成一维数组
  343. $addonRow = array();
  344. // 根据不同字段类型封装数据
  345. $list = $this->showViewFormData($row, 'users_', $addonRow);
  346. return $list;
  347. }
  348. /**
  349. * 查询解析数据表的数据用以构造from表单
  350. * @param return $list
  351. * @param 用于修改,携带数据
  352. * @author 陈风任 by 2019-2-20
  353. */
  354. public function getDataParaList($users_id = '', $is_system = '')
  355. {
  356. // 字段及内容数据处理
  357. $where = [
  358. 'a.lang' => $this->home_lang,
  359. 'a.is_hidden' => 0,
  360. ];
  361. if (!empty($is_system)) {
  362. $where['a.is_system'] = 1;
  363. }
  364. //删除多余干扰数据
  365. $have = Db::name("users_list")->field("max(list_id) as list_id")->where(['lang'=>$this->home_lang,"users_id"=>$users_id])->group("para_id")->getField("list_id",true);
  366. if ($have){
  367. Db::name("users_list")->where(['lang'=>$this->home_lang,"users_id"=>$users_id,"list_id"=>['not in',$have]])->delete();
  368. }
  369. $row = Db::name('users_parameter')->alias('a')->where($where)->order('a.sort_order asc,a.para_id asc')->cache(true, EYOUCMS_CACHE_TIME, 'users_parameter')->select();
  370. $listRow = Db::name('users_list')->field('info,para_id,users_id')->where(['users_id'=>$users_id])->getAllWithIndex('para_id');
  371. foreach ($row as $key => $val) {
  372. $val['users_id'] = empty($listRow[$val['para_id']]) ? '' : $listRow[$val['para_id']]['users_id'];
  373. $val['info'] = empty($listRow[$val['para_id']]) ? '' : $listRow[$val['para_id']]['info'];
  374. $row[$key] = $val;
  375. }
  376. // 根据所需数据格式,拆分成一维数组
  377. $addonRow = [];
  378. foreach ($row as $key => $value) {
  379. $addonRow[$value['name']] = $value['info'];
  380. }
  381. // 根据不同字段类型封装数据
  382. $list = $this->showViewFormData($row, 'users_', $addonRow);
  383. return $list;
  384. }
  385. /**
  386. * 处理页面显示字段的表单数据
  387. * @param array $list 字段列表
  388. * @param array $formFieldStr 表单元素名称的统一数组前缀
  389. * @param array $addonRow 字段的数据
  390. * @author 陈风任 by 2019-2-20
  391. */
  392. public function showViewFormData($list, $formFieldStr, $addonRow = array())
  393. {
  394. if (!empty($list)) {
  395. foreach ($list as $key => $val) {
  396. $val['fieldArr'] = $formFieldStr;
  397. switch ($val['dtype']) {
  398. case 'int':
  399. {
  400. if (isset($addonRow[$val['name']])) {
  401. $val['dfvalue'] = $addonRow[$val['name']];
  402. } else {
  403. if(preg_match("#[^0-9]#", $val['dfvalue']))
  404. {
  405. $val['dfvalue'] = "";
  406. }
  407. }
  408. break;
  409. }
  410. case 'float':
  411. case 'decimal':
  412. {
  413. if (isset($addonRow[$val['name']])) {
  414. $val['dfvalue'] = $addonRow[$val['name']];
  415. } else {
  416. if(preg_match("#[^0-9\.]#", $val['dfvalue']))
  417. {
  418. $val['dfvalue'] = "";
  419. }
  420. }
  421. break;
  422. }
  423. case 'select':
  424. {
  425. $dfvalue = $val['dfvalue'];
  426. $dfvalueArr = explode(',', $dfvalue);
  427. $val['dfvalue'] = $dfvalueArr;
  428. if (isset($addonRow[$val['name']])) {
  429. $val['trueValue'] = explode(',', $addonRow[$val['name']]);
  430. } else {
  431. $dfTrueValue = !empty($dfvalueArr[0]) ? $dfvalueArr[0] : '';
  432. $val['trueValue'] = array();
  433. }
  434. break;
  435. }
  436. case 'radio':
  437. {
  438. $dfvalue = $val['dfvalue'];
  439. $dfvalueArr = explode(',', $dfvalue);
  440. $val['dfvalue'] = $dfvalueArr;
  441. if (isset($addonRow[$val['name']])) {
  442. $val['trueValue'] = explode(',', $addonRow[$val['name']]);
  443. } else {
  444. $dfTrueValue = !empty($dfvalueArr[0]) ? $dfvalueArr[0] : '';
  445. $val['trueValue'] = array($dfTrueValue);
  446. }
  447. break;
  448. }
  449. case 'checkbox':
  450. {
  451. $dfvalue = $val['dfvalue'];
  452. $dfvalueArr = explode(',', $dfvalue);
  453. $val['dfvalue'] = $dfvalueArr;
  454. if (isset($addonRow[$val['name']])) {
  455. $val['trueValue'] = explode(',', $addonRow[$val['name']]);
  456. } else {
  457. $val['trueValue'] = array();
  458. }
  459. break;
  460. }
  461. case 'img':
  462. {
  463. if (isset($addonRow[$val['name']])) {
  464. $val[$val['name']] = handle_subdir_pic($addonRow[$val['name']]);
  465. $val['info'] = handle_subdir_pic($addonRow[$val['name']]);
  466. }
  467. break;
  468. }
  469. case 'imgs':
  470. {
  471. $val[$val['name'].'_eyou_imgupload_list'] = array();
  472. if (isset($addonRow[$val['name']]) && !empty($addonRow[$val['name']])) {
  473. $eyou_imgupload_list = explode(',', $addonRow[$val['name']]);
  474. /*支持子目录*/
  475. foreach ($eyou_imgupload_list as $k1 => $v1) {
  476. $eyou_imgupload_list[$k1] = handle_subdir_pic($v1);
  477. }
  478. /*--end*/
  479. $val[$val['name'].'_eyou_imgupload_list'] = $eyou_imgupload_list;
  480. }
  481. break;
  482. }
  483. case 'file':
  484. {
  485. if (isset($addonRow[$val['name']])) {
  486. $val[$val['name']] = handle_subdir_pic($addonRow[$val['name']]);
  487. }
  488. $ext = tpCache('basic.file_type');
  489. $val['ext'] = !empty($ext) ? $ext : "zip|gz|rar|iso|doc|xls|ppt|wps";
  490. $val['filesize'] = upload_max_filesize();
  491. break;
  492. }
  493. case 'datetime':
  494. {
  495. if (!empty($addonRow[$val['name']])) {
  496. if (is_numeric($addonRow[$val['name']])) {
  497. $val['dfvalue'] = date('Y-m-d H:i:s', $addonRow[$val['name']]);
  498. } else {
  499. $val['dfvalue'] = $addonRow[$val['name']];
  500. }
  501. } else {
  502. $val['dfvalue'] = date('Y-m-d H:i:s');
  503. }
  504. break;
  505. }
  506. case 'htmltext':
  507. {
  508. $val['dfvalue'] = isset($addonRow[$val['name']]) ? $addonRow[$val['name']] : $val['dfvalue'];
  509. /*支持子目录*/
  510. $val['dfvalue'] = handle_subdir_pic($val['dfvalue'], 'html');
  511. /*--end*/
  512. break;
  513. }
  514. default:
  515. {
  516. $val['dfvalue'] = isset($addonRow[$val['name']]) ? $addonRow[$val['name']] : $val['dfvalue'];
  517. /*支持子目录*/
  518. if (is_string($val['dfvalue'])) {
  519. $val['dfvalue'] = handle_subdir_pic($val['dfvalue'], 'html');
  520. $val['dfvalue'] = handle_subdir_pic($val['dfvalue']);
  521. }
  522. /*--end*/
  523. break;
  524. }
  525. }
  526. $list[$key] = $val;
  527. }
  528. }
  529. return $list;
  530. }
  531. }