SettingForm.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Admin;
  9. use app\models\Option;
  10. use yii\base\Model;
  11. use app\models\Store;
  12. use app\constants\OptionSetting;
  13. class SettingForm extends Model
  14. {
  15. public $data = [];
  16. public function save($store_id = 0)
  17. {
  18. $oldQQMapKey = Option::get(OptionSetting::TENCENT_MAP_KEY, DEFAULT_STORE_ID, 'pay', Option::get(OptionSetting::TENCENT_MAP_KEY, DEFAULT_STORE_ID, 'store', '')['value'])['value'];
  19. if ($this->data) {
  20. foreach ($this->data as $val) {
  21. if (count($val['list']) > 0) {
  22. $res = Option::verifyData($val['list'], $val['key']);
  23. if ($res && $res['code'] > 0) {
  24. return $res;
  25. }
  26. }
  27. }
  28. }
  29. $t = \Yii::$app->db->beginTransaction();
  30. foreach ($this->data as $val) {
  31. $res = Option::setGroup($val['key'],$val['list'], $store_id ?: \get_store_id());
  32. if ( $res !== true) {
  33. $t->rollBack();
  34. return [
  35. 'code' => 1,
  36. 'msg' => '保存失败',
  37. 'err' => $res
  38. ];
  39. }
  40. }
  41. $this->upStore($store_id);
  42. $t->commit();
  43. \app\utils\UpdateAfter::UpdateQQMapKey($oldQQMapKey);
  44. return [
  45. 'code' => 0,
  46. 'msg' => '保存成功'
  47. ];
  48. }
  49. private function upStore($store_id = 0){
  50. $store_id = $store_id ? $store_id : get_store_id();
  51. $store = Store::findOne($store_id);
  52. $name = Option::get('name', $store->id);
  53. $logo = Option::get('logo', $store->id);
  54. $coordinate = Option::get('coordinate', $store->id);
  55. $address_info = Option::get('address_info', $store->id);
  56. $address = Option::get('address', $store->id);
  57. $address_info = json_decode($address_info['value'], true);
  58. if (!empty($address_info)) {
  59. $store->province_id = $address_info['province_id'];
  60. $store->city_id = $address_info['city_id'];
  61. $store->district_id = $address_info['district_id'];
  62. }
  63. if (!empty($address['value'])) {
  64. $store->address = $address['value'];
  65. }
  66. if (!empty($coordinate['value'])) {
  67. $store->coordinate = $coordinate['value'];
  68. $coordinate = explode(',', $coordinate['value']);
  69. $store->longitude = $coordinate[1];
  70. $store->latitude = $coordinate[0];
  71. }
  72. if ($name['value']) {
  73. $store->name = $name['value'];
  74. }
  75. if ($logo['value']) {
  76. $store->logo = $logo['value'];
  77. }
  78. $contact_tel = Option::get('contact_tel', $store->id)['value'];
  79. $store->contact_tel = $contact_tel;
  80. $store->save();
  81. $admin = Admin::findOne(['OR', ['type' => 'store', 'type_id' => $store->id, 'is_delete' => 0], ['id' => $store->admin_id]]);
  82. if ($admin) {
  83. $admin->mobile = $contact_tel;
  84. $admin->save();
  85. }
  86. }
  87. public static function staticUpStore($store_id) {
  88. $self = new self();
  89. $self->upStore($store_id);
  90. }
  91. }