UserValidate.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\validate;
  20. use app\common\model\User;
  21. use app\common\validate\BaseValidate;
  22. /**
  23. * 用户验证器
  24. * Class UserValidate
  25. * @package app\shopapi\validate
  26. */
  27. class UserValidate extends BaseValidate
  28. {
  29. protected $rule = [
  30. 'pay_password' => 'require|checkPayPassword',
  31. 'transfer_in' => 'require|checkTransferIn',
  32. 'money' => 'require|gt:0|checkMoney',
  33. 'code' => 'require',
  34. 'encrypted_data' => 'require',
  35. 'iv' => 'require',
  36. 'mobile' => 'require|mobile',
  37. 'password' => 'require|length:6,20|alphaDash',
  38. 'old_password' => 'require',
  39. ];
  40. protected $message = [
  41. 'pay_password.require' => '请输入支付密码',
  42. 'pay_password.length' => '支付密码须在4-8位之间',
  43. 'pay_password.alphaDash' => '支付密码须为字母数字下划线或破折号',
  44. 'transfer_in.require' => '请选择收款用户',
  45. 'money.require' => '请输入转账金额',
  46. 'money.gt' => '转账金额须大于0',
  47. 'code.require' => '参数缺失',
  48. 'encrypted_data.require' => '参数缺失',
  49. 'iv.require' => '参数缺失',
  50. 'mobile.require' => '请输入手机号码',
  51. 'mobile.mobile' => '无效的手机号码',
  52. 'old_password.require' => '请输入原密码',
  53. 'password.require' => '请输入登录密码',
  54. 'password.length' => '登录密码须在6-20位之间',
  55. 'password.alphaDash' => '登录密码须为字母数字下划线或破折号',
  56. ];
  57. /**
  58. * @notes 余额转账场景
  59. * @return UserValidate
  60. * @author Tab
  61. * @date 2021/8/11 20:27
  62. */
  63. public function sceneTransfer()
  64. {
  65. return $this->only(['pay_password', 'transfer_in', 'money']);
  66. }
  67. /**
  68. * @notes 收款用户信息场景
  69. * @return UserValidate
  70. * @author Tab
  71. * @date 2021/8/12 10:53
  72. */
  73. public function sceneTransferIn()
  74. {
  75. return $this->only(['transfer_in'])
  76. ->remove('transfer_in', 'checkTransferIn');
  77. }
  78. /**
  79. * @notes 设置/修改交易密码场景
  80. * @return UserValidate
  81. * @author Tab
  82. * @date 2021/8/12 11:12
  83. */
  84. public function sceneSetPayPassword()
  85. {
  86. return $this->only(['pay_password'])
  87. ->remove('pay_password', 'checkPayPassword')
  88. ->append('pay_password', 'length:4,8|alphaDash');
  89. }
  90. /**
  91. * @notes 获取微信手机号场景
  92. * @return UserValidate
  93. * @author Tab
  94. * @date 2021/8/24 15:13
  95. */
  96. public function sceneGetMobileByMnp()
  97. {
  98. return $this->only(['code']);
  99. }
  100. /**
  101. * @notes 发送验证码 - 重置支付密码
  102. * @return UserValidate
  103. * @author Tab
  104. * @date 2021/8/24 15:52
  105. */
  106. public function sceneResetPayPasswordCaptcha()
  107. {
  108. return $this->only(['mobile']);
  109. }
  110. /**
  111. * @notes 发送验证码 - 重置登录密码
  112. * @return UserValidate
  113. * @author Tab
  114. * @date 2021/8/25 16:34
  115. */
  116. public function sceneResetPasswordCaptcha()
  117. {
  118. return $this->only(['mobile']);
  119. }
  120. /**
  121. * @notes 发送验证码 - 绑定手机号
  122. * @return UserValidate
  123. * @author Tab
  124. * @date 2021/8/25 16:34
  125. */
  126. public function sceneBindMobileCaptcha()
  127. {
  128. return $this->only(['mobile']);
  129. }
  130. /**
  131. * @notes 发送验证码 - 变更手机号
  132. * @return UserValidate
  133. * @author Tab
  134. * @date 2021/8/25 16:34
  135. */
  136. public function sceneChangeMobileCaptcha()
  137. {
  138. return $this->only(['mobile']);
  139. }
  140. /**
  141. * @notes 绑定手机号场景
  142. * @return UserValidate
  143. * @author Tab
  144. * @date 2021/8/25 17:44
  145. */
  146. public function sceneBindMobile()
  147. {
  148. return $this->only(['mobile', 'code']);
  149. }
  150. /**
  151. * @notes 重置支付密码
  152. * @return UserValidate
  153. * @author Tab
  154. * @date 2021/8/24 16:14
  155. */
  156. public function sceneResetPayPassword()
  157. {
  158. return $this->only(['mobile', 'code', 'pay_password'])
  159. ->remove('pay_password', 'checkPayPassword')
  160. ->append('pay_password', 'length:4,8|alphaDash');
  161. }
  162. /**
  163. * @notes 重置登录密码
  164. * @return UserValidate
  165. * @author Tab
  166. * @date 2021/8/25 16:37
  167. */
  168. public function sceneResetPassword()
  169. {
  170. return $this->only(['mobile', 'code', 'password'])
  171. ->append('password', 'require|length:6,12|alphaDash|checkComplexity');
  172. }
  173. /**
  174. * @notes 修改密码
  175. * @return UserValidate
  176. * @author cjhao
  177. * @date 2023/3/7 16:27
  178. */
  179. public function sceneChangePassword()
  180. {
  181. return $this->only(['old_password','password'])
  182. ->append('password','require|length:6,20|alphaDash|checkComplexity');
  183. }
  184. /**
  185. * @notes 设置登录密码
  186. * @return UserValidate
  187. * @author Tab
  188. */
  189. public function sceneSetPassword()
  190. {
  191. return $this->only(['password'])
  192. ->append('password','require|length:6,20|alphaDash|checkComplexity');
  193. }
  194. /**
  195. * @notes 校验支付密码
  196. * @param $value
  197. * @param $rule
  198. * @param $data
  199. * @return bool|string
  200. * @author Tab
  201. * @date 2021/8/11 20:35
  202. */
  203. public function checkPayPassword($value, $rule, $data)
  204. {
  205. $user = User::findOrEmpty($data['user_id']);
  206. if(empty($user->mobile)) {
  207. return '请先绑定手机号';
  208. }
  209. if(empty($user->pay_password)) {
  210. return '请先设置支付密码';
  211. }
  212. if($user->pay_password == md5($value)) {
  213. return true;
  214. }
  215. return '支付密码错误';
  216. }
  217. /**
  218. * @notes 校验收款用户
  219. * @param $value
  220. * @param $rule
  221. * @param $data
  222. * @return bool|string
  223. * @author Tab
  224. * @date 2021/8/11 20:38
  225. */
  226. public function checktransferIn($value, $rule, $data)
  227. {
  228. $me = User::findOrEmpty($data['user_id']);
  229. if($me->sn == $data['transfer_in'] || $me->mobile == $data['transfer_in']) {
  230. return '不能自己转账给自己';
  231. }
  232. $transferIn = User::whereOr('sn', $data['transfer_in'])
  233. ->whereOr('mobile', $data['transfer_in'])
  234. ->findOrEmpty();
  235. if($transferIn->isEmpty()) {
  236. return '收款用户不存在';
  237. }
  238. return true;
  239. }
  240. /**
  241. * @notes 校验余额
  242. * @param $value
  243. * @param $rule
  244. * @param $data
  245. * @return bool|string
  246. * @author Tab
  247. * @date 2021/8/11 20:42
  248. */
  249. public function checkMoney($value, $rule, $data)
  250. {
  251. $user = User::findOrEmpty($data['user_id']);
  252. if($user->user_money < $data['money']) {
  253. return '余额不足';
  254. }
  255. return true;
  256. }
  257. /**
  258. * @notes 校验密码复杂度
  259. * @param $value
  260. * @param $rue
  261. * @param $data
  262. * @author Tab
  263. * @date 2021/12/10 15:06
  264. */
  265. public function checkComplexity($value, $rue, $data)
  266. {
  267. $lowerCase = range('a', 'z');
  268. $upperCase = range('A', 'Z');
  269. $numbers = range(0, 9);
  270. $cases = array_merge($lowerCase, $upperCase);
  271. $caseCount = 0;
  272. $numberCount = 0;
  273. $passwordArr = str_split(trim(($data['password'] . '')));
  274. foreach ($passwordArr as $value) {
  275. if (in_array($value, $numbers)) {
  276. $numberCount++;
  277. }
  278. if (in_array($value, $cases)) {
  279. $caseCount++;
  280. }
  281. }
  282. if ($numberCount >= 1 && $caseCount >= 1) {
  283. return true;
  284. }
  285. return '密码需包含数字和字母';
  286. }
  287. }