UploadForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Cache;
  4. use common\components\Model;
  5. use common\helpers\Date;
  6. use common\helpers\Excel;
  7. use common\helpers\Form;
  8. use common\helpers\http\RemoteUploadApi;
  9. use common\helpers\ocr\OcrApi;
  10. use common\helpers\user\Info;
  11. use common\models\ExcelAddUser;
  12. use common\models\ExcelImport;
  13. use common\models\InvoiceAudit;
  14. use common\models\Recharge;
  15. use common\models\Uploads;
  16. use common\models\User;
  17. use common\models\Withdraw;
  18. use yii\base\Exception;
  19. /**
  20. * Login form
  21. */
  22. class UploadForm extends Model {
  23. public $file;
  24. public $excelOption; // 导入的excel文件用来干什么,存入excel导入表
  25. public $token;
  26. public $remark;
  27. public $withdrawId;
  28. public $rechargeId;
  29. private $_ocrResult;
  30. private $_remoteScenario = [
  31. // 'idCardFront', 'idCardBack', 'ad', 'invoiceFront','proveFront','goodsImg'
  32. ];
  33. /**
  34. * @inheritdoc
  35. */
  36. public function rules() {
  37. return [
  38. [['remark', 'withdrawId', 'rechargeId'], 'trim'],
  39. [['file', 'token', 'excelOption'], 'required'],
  40. [['token'], 'isToken'],
  41. [['file'], 'file'],
  42. [['file'], 'file', 'mimeTypes' => ['image/jpeg', 'image/png', 'image/x-png'], 'on' => ['idCardFront', 'idCardBack', 'ad']],
  43. // todo 暂时屏蔽
  44. // [['file'], 'file', 'mimeTypes'=>['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv'], 'on'=>['excel']],
  45. [['file'], 'isIdCardFront', 'on' => ['idCardFront']],
  46. [['file'], 'isInvoiceFront', 'on' => ['invoiceFront']],
  47. //[['file'], 'isIdCardBack', 'on'=>['idCardBack']],
  48. ];
  49. }
  50. /**
  51. * 指定校验场景
  52. * @return array
  53. */
  54. public function scenarios() {
  55. $parentScenarios = parent::scenarios();
  56. $customScenarios = [
  57. 'idCardFront' => ['file', 'token'],
  58. 'idCardBack' => ['file', 'token'],
  59. 'invoiceFront' => ['file', 'token', 'remark', 'withdrawId'],
  60. 'proveFront' => ['file', 'token', 'rechargeId'],
  61. 'ad' => ['file', 'token'],
  62. 'excel' => ['file', 'token', 'excelOption'],
  63. 'goodsImg' => ['file', 'token'],
  64. ];
  65. return array_merge($parentScenarios, $customScenarios);
  66. }
  67. public function attributeLabels() {
  68. return [
  69. 'file' => '文件',
  70. ];
  71. }
  72. /**
  73. * 校验上传token
  74. * @param $attributes
  75. */
  76. public function isToken($attributes) {
  77. if (!Cache::getUploadToken($this->token)) {
  78. $this->addError($attributes, '上传token校验失败');
  79. }
  80. }
  81. /**
  82. * 是否是身份证
  83. * @param $attributes
  84. */
  85. public function isIdCardFront($attributes) {
  86. // 查看该用户是否已经上传过身份证照片
  87. $oneData = User::find()->select('ID_IMAGE')->where('ID=:ID', [':ID' => \Yii::$app->user->id])->asArray()->one();
  88. if ($oneData['ID_IMAGE']) {
  89. $this->addError($attributes, '您已上传过身份证照片');
  90. }
  91. $this->_ocrResult = OcrApi::instance()->idCard($this->file->tempName);
  92. if (!$this->_ocrResult['success']) {
  93. $this->addError($attributes, $this->_ocrResult['message']);
  94. }
  95. }
  96. /**
  97. * 前台上传发票
  98. * @param $attributes
  99. */
  100. public function isInvoiceFront($attributes) {
  101. $this->_ocrResult = OcrApi::instance()->vatInvoice($this->file->tempName);
  102. if (!$this->_ocrResult['success']) {
  103. $this->addError($attributes, $this->_ocrResult['message']);
  104. }
  105. }
  106. /**
  107. * 上传
  108. * @return bool
  109. * @throws \yii\db\Exception
  110. */
  111. public function upload() {
  112. if (!$this->validate()) {
  113. return false;
  114. }
  115. $oneUser = User::findOne(['ID' => \Yii::$app->user->id]);
  116. $db = \Yii::$app->db;
  117. $transaction = $db->beginTransaction();
  118. try {
  119. switch ($this->scenario) {
  120. case 'invoiceFront':
  121. $uploadCategory = Uploads::CATEGORY_INVOICE;
  122. $uploadRemark = $this->remark;
  123. break;
  124. case 'idCardFront':
  125. $uploadCategory = Uploads::CATEGORY_ID_CARD;
  126. $uploadRemark = $this->_ocrResult['realName'] . '身份证正面';
  127. break;
  128. case 'idCardBack':
  129. $uploadCategory = Uploads::CATEGORY_ID_CARD;
  130. $uploadRemark = $oneUser['REAL_NAME'] . '身份证背面';
  131. break;
  132. case 'proveFront':
  133. case 'goodsImg':
  134. case 'ad':
  135. $uploadCategory = Uploads::CATEGORY_IMAGE;
  136. $uploadRemark = $this->file->baseName;
  137. break;
  138. case 'excel':
  139. $uploadCategory = Uploads::CATEGORY_EXCEL;
  140. $uploadRemark = $this->file->baseName;
  141. break;
  142. default :
  143. $uploadCategory = Uploads::CATEGORY_UN_KNOW;
  144. $uploadRemark = $this->file->baseName;
  145. break;
  146. }
  147. // 上传图片到远程服务器
  148. if (in_array($this->scenario, $this->_remoteScenario)) {
  149. $remoteUploadApi = RemoteUploadApi::instance();
  150. if ($uploadResult = $remoteUploadApi->upload($this->file->tempName)) {
  151. $uploadInfo = [
  152. 'fileName' => $uploadResult['name'],
  153. 'category' => $uploadCategory,
  154. 'url' => $uploadResult['url'],
  155. 'fileSize' => $uploadResult['size'] ?? null,
  156. 'md5' => $uploadResult['md5'] ?? null,
  157. ];
  158. } else {
  159. throw new Exception('文件远程上传失败');
  160. }
  161. // 删除本地临时文件
  162. unlink($this->file->tempName);
  163. } else {
  164. // 保存在本地
  165. $localPath = \Yii::getAlias('@common/runtime/uploads/') . $this->file->baseName . '.' . $this->file->extension;
  166. if (!$this->file->saveAs($localPath)) {
  167. throw new Exception('文件保存失败');
  168. }
  169. $uploadInfo = [
  170. 'fileName' => $this->file->baseName . '.' . $this->file->extension,
  171. 'category' => $uploadCategory,
  172. // 'url' => $localPath,
  173. 'url' => $this->file->baseName . '.' . $this->file->extension,
  174. 'fileSize' => null,
  175. 'md5' => null,
  176. ];
  177. }
  178. // 把上传的文件写入数据库记录中
  179. // 把文件对应的相关资料存入数据库中
  180. $uploads = new Uploads();
  181. $uploads->FILE_NAME = $uploadInfo['fileName'];
  182. $uploads->CATEGORY = $uploadInfo['category'];
  183. $uploads->URL = $uploadInfo['url'];
  184. $uploads->FILE_SIZE = $uploadInfo['fileSize'] ?? null;
  185. $uploads->MD5 = $uploadInfo['md5'] ?? null;
  186. $uploads->REMARK = $uploadRemark;
  187. $uploads->CREATED_AT = Date::nowTime();
  188. if (!$uploads->save()) {
  189. throw new Exception('上传文件信息保存失败');
  190. }
  191. // 如果是上传发票,更新发票信息,并绑定提现记录
  192. if ($this->scenario == 'invoiceFront') {
  193. $withdraw = Withdraw::findOne(['ID' => $this->withdrawId]);
  194. //判断金额是否一致
  195. if ($withdraw['AMOUNT'] != $this->_ocrResult['amount']) {
  196. throw new Exception('上传发票金额'.$this->_ocrResult['amount'].'与提现金额【'.$withdraw['AMOUNT'].'】不一致');
  197. }
  198. //发票内容是否与后台预置信息相符
  199. $configName = Cache::getSystemConfig()['invoiceItemName']['VALUE'];
  200. if (!in_array($this->_ocrResult['itemName'], explode("\n",$configName))) {
  201. throw new Exception('上传发票中货物或应税劳务服务名称与后台预置信息【' . $configName . '】不一致');
  202. }
  203. //写入发票数据
  204. if (InvoiceAudit::find()->where('INVOICE_NUM=:INVOICE_NUM AND WITHDRAW_ID!=:WITHDRAW_ID AND AUDIT_STATUS!=:AUDIT_STATUS', [':INVOICE_NUM' => $this->_ocrResult['invoiceNum'], ':WITHDRAW_ID' => $this->withdrawId, ':AUDIT_STATUS' => \Yii::$app->params['auditStatus']['reject']['value']])->exists()) {
  205. throw new Exception('该发票已被使用');
  206. }
  207. if(!$invoiceModel = InvoiceAudit::findOne(['WITHDRAW_ID' => $this->withdrawId])){
  208. $invoiceModel = new InvoiceAudit();
  209. }
  210. $invoiceModel->USER_ID = $oneUser['ID'];
  211. $invoiceModel->WITHDRAW_ID = $this->withdrawId;
  212. $invoiceModel->INVOICE_CODE = $this->_ocrResult['invoiceCode'];
  213. $invoiceModel->INVOICE_NUM = $this->_ocrResult['invoiceNum'];
  214. $invoiceModel->INVOICE_DATE = $this->_ocrResult['invoiceDate'];
  215. $invoiceModel->AMOUNT = $this->_ocrResult['amount'];
  216. $invoiceModel->TAX_RATE = $this->_ocrResult['taxRate'];
  217. $invoiceModel->PURCHASER_NAME = $this->_ocrResult['purchaserName'];
  218. $invoiceModel->PURCHASER_REGISTER_NUM = $this->_ocrResult['purchaserRegisterNum'];
  219. $invoiceModel->PURCHASER_ADDRESS = $this->_ocrResult['purchaserAddress'];
  220. $invoiceModel->PURCHASER_BANK = $this->_ocrResult['purchaserBank'];
  221. $invoiceModel->SELLER_NAME = $this->_ocrResult['sellerName'];
  222. $invoiceModel->SELLER_REGISTER_NUM = $this->_ocrResult['sellerRegisterNum'];
  223. $invoiceModel->SELLER_ADDRESS = $this->_ocrResult['sellerAddress'];
  224. $invoiceModel->SELLER_BANK = $this->_ocrResult['sellerBank'];
  225. $invoiceModel->ITEM_NAME = $this->_ocrResult['itemName'];
  226. $invoiceModel->INVOICE_REMARK = $this->_ocrResult['invoiceRemark'];
  227. $invoiceModel->UPLOAD_ID = $uploads->ID;
  228. $invoiceModel->CREATED_AT = Date::nowTime();
  229. if (!$invoiceModel->save()) {
  230. throw new Exception(Form::formatErrorsForApi($invoiceModel->getErrors()));
  231. }
  232. //写入提现表
  233. $withdraw->INVOICE_ID = $invoiceModel->ID;
  234. $withdraw->AUDIT_STATUS = Withdraw::STATUS_APPLIED;
  235. $withdraw->UPDATED_AT = Date::nowTime();
  236. if (!$withdraw->save()) {
  237. throw new Exception(Form::formatErrorsForApi($withdraw->getErrors()));
  238. }
  239. } // 如果是上传身份证正面的场景,则把识别出来的信息更新当前的会员信息
  240. elseif ($this->scenario == 'idCardFront') {
  241. // 修改用户基本信息
  242. $oneUser->REAL_NAME = $this->_ocrResult['realName'];
  243. $oneUser->ID_CARD = $this->_ocrResult['idCardNo'];
  244. $oneUser->ADDRESS = $this->_ocrResult['address'];
  245. $oneUser->NATION = Info::getNationCode($this->_ocrResult['nation']);
  246. $oneUser->ID_IMAGE = $uploadInfo['url'];
  247. if (!$oneUser->save()) {
  248. // throw new Exception('用户基本资料保存失败');
  249. throw new Exception(Form::formatErrorsForApi($oneUser->getErrors()));
  250. }
  251. // 更新会员信息缓存
  252. User::updateBaseInfoToRedis($oneUser['ID']);
  253. } elseif ($this->scenario == 'excel') {
  254. $excelTableClass = Excel::EXCEL_STRUCTURE[$this->excelOption]['excelTableClass'];
  255. // 把上传的文件写入到导入记录中
  256. $excelImport = new ExcelImport();
  257. $excelImport->OPTION_NAME = $this->excelOption;
  258. $excelImport->TABLE_NAME = $excelTableClass::tableName();
  259. $excelImport->UPLOAD_ID = $uploads->ID;
  260. $excelImport->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  261. $excelImport->IMPORT_ADMIN_ID = \Yii::$app->user->id;
  262. $excelImport->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  263. $excelImport->CREATED_AT = Date::nowTime();
  264. if (!$excelImport->save()) {
  265. throw new Exception(Form::formatErrorsForApi($excelImport->getErrors()));
  266. }
  267. }elseif ($this->scenario == 'proveFront') {
  268. // 如果是上传充值凭证,绑定充值记录
  269. $recharge = Recharge::findOne(['ID' => $this->rechargeId]);
  270. if(!$recharge){
  271. throw new Exception("缺少参数ID");
  272. }
  273. //写入充值表
  274. $recharge->BANK_PROVE = $uploadInfo['url'];
  275. $recharge->AUDIT_STATUS = Recharge::STATUS_PROVED;
  276. if (!$recharge->save()) {
  277. throw new Exception(Form::formatErrorsForApi($recharge->getErrors()));
  278. }
  279. }
  280. $transaction->commit();
  281. } catch (Exception $e) {
  282. $transaction->rollBack();
  283. $this->addError('upload', $e->getMessage());
  284. return false;
  285. }
  286. return $uploads;
  287. }
  288. }