NewDiyForm.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\DiyTemplate;
  9. use app\models\NewDiyTemplate;
  10. use app\models\Option;
  11. use app\models\Store;
  12. use yii\base\Model;
  13. use yii\helpers\Json;
  14. class NewDiyForm extends Model
  15. {
  16. /**
  17. * @return array
  18. */
  19. public function rules()
  20. {
  21. return [
  22. [[]]
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. ];
  29. }
  30. /**
  31. * 新diy
  32. * @return array
  33. */
  34. public static function saveNewTemplate()
  35. {
  36. $template = post_params('template');
  37. if ($template == '{}') {
  38. return [
  39. 'code' => 1,
  40. 'msg' => '保存模板数据为空'
  41. ];
  42. }
  43. $controls = post_params('controls');
  44. $id = post_params('id', 0);
  45. $name = post_params('name', "DIY");
  46. $is_index = post_params('is_index', 0);
  47. $level = post_params('level',-1);// 默认是普通用户
  48. $type = post_params('type', 0);
  49. if ($is_index) {
  50. NewDiyTemplate::updateAll(['is_index' => 0], ['store_id' => get_store_id(), 'is_delete' => 0]);
  51. // 如果是首页的话 $template中的level字段设置为0 -1是普通用户 0是首页不判断 大于0是会员等级的用户
  52. $level = 0;// 首页用0
  53. }
  54. $form = NewDiyTemplate::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'id' => $id]) ?: new NewDiyTemplate();
  55. $form->store_id = get_store_id();
  56. $form->name = $name;
  57. $form->template = $template;
  58. $form->is_delete = 0;
  59. $form->is_index = $is_index;
  60. $form->addtime = time();
  61. $form->type = $type;
  62. $form->level = $level;
  63. if ($form->save()) {
  64. // 保存控件
  65. if ($controls) {
  66. self::saveControls($controls);
  67. }
  68. return [
  69. 'code' => 0,
  70. 'msg' => '保存成功',
  71. 'data' => [
  72. 'template' => $form,
  73. 'controls' => NewDiyTemplate::find()
  74. ->where([
  75. 'store_id' => get_store_id(),
  76. 'is_delete' => 0,
  77. 'type' => 100
  78. ])->asArray()->one()['template']
  79. ]
  80. ];
  81. } else {
  82. return [
  83. 'code' => 1,
  84. 'msg' => $form->errors
  85. ];
  86. }
  87. }
  88. public static function saveControls($controls)
  89. {
  90. // controls type=100
  91. $controlsDiy = NewDiyTemplate::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'type' => 100]) ?: new NewDiyTemplate();
  92. $controlsDiy->store_id = get_store_id();
  93. $controlsDiy->name = 'controls';
  94. $controlsDiy->template = $controls;
  95. $controlsDiy->is_delete = 0;
  96. $controlsDiy->addtime = time();
  97. $controlsDiy->type = 100;
  98. $controlsDiy->save();
  99. }
  100. public static function getNewTemplateList()
  101. {
  102. $query = NewDiyTemplate::find()
  103. ->where([
  104. 'store_id' => get_store_id(),
  105. 'is_delete' => 0,
  106. ]);
  107. $list = $query->andWhere(['!=', 'type', 100])->orderBy(['id' => SORT_ASC])->asArray()->all();
  108. foreach ($list as &$item) {
  109. $template = json_decode($item['template'], true);
  110. if ($template['system_type'] === 'goodsDetail') {
  111. foreach ($template['templates'] as &$tem) {
  112. if ($tem['comName'] === "goodsSwiperImgs") {
  113. if (!isset($tem['params']['showFenxiao'])) {
  114. $tem['params'] = array_merge($tem['params'], [
  115. 'showFenxiao' => true,
  116. 'margin' => 0
  117. ]);
  118. }
  119. }
  120. }
  121. }
  122. $item['template'] = json_encode($template, JSON_UNESCAPED_UNICODE);
  123. }
  124. // 这里需要查询发挥是否商城开启以图搜图功能
  125. $imageSearchStatus = (int)Option::get('tencent_image_setting_status',0,'saas',0)['value'];
  126. $store = Store::findOne(get_store_id());
  127. $controls = NewDiyTemplate::find()
  128. ->where([
  129. 'store_id' => get_store_id(),
  130. 'is_delete' => 0,
  131. 'type' => 100
  132. ])->asArray()->one()['template'];
  133. if ($store && intval($store->business_model) !== 1 && get_store_id() != -1) {
  134. if ($controls) {
  135. $controls = json_decode($controls, true);
  136. unset($item);
  137. foreach ($controls as $index => $item) {
  138. if ($item['show_type'] === 'nav') {
  139. unset($controls[$index]);
  140. }
  141. }
  142. $controls = json_encode(array_values($controls), JSON_UNESCAPED_UNICODE);
  143. }
  144. }
  145. if (get_store_id() > 0 && \Yii::$app->prod_is_dandianpu()) {//供应链系统未使用小程序店铺且使用共享流量店铺开关时候 就增加联盟商家
  146. $self_mini = Option::get('self_mini', get_store_id(), 'store', 0)['value'];
  147. $is_store_traffic_sharing = Option::get('is_store_traffic_sharing', get_store_id(), 'store', 1)['value'];
  148. $switch_store = intval(Option::get('one_store_switch_store', 0, 'saas', 0)['value']);
  149. $is_store_traffic_sharing_switch = false;
  150. if (intval($is_store_traffic_sharing) && !intval($self_mini) && $switch_store) {
  151. $is_store_traffic_sharing_switch = true;
  152. }
  153. $controls_arr = json_decode($controls, true);
  154. foreach ($controls_arr as &$control_item) {
  155. if ($control_item['controlComName'] == 'widgetNavControl') {
  156. $tab_open = true;
  157. foreach ($control_item['params']['navList'] as $nav_index => &$nav_item) {
  158. if (isset($nav_item['tab_type']) && $nav_item['tab_type'] == 'store_sharing') {
  159. $nav_item['link']['link'] = '/allianceStore/index/index';
  160. $nav_item['link']['open_type'] = 'navigate';
  161. if (!$is_store_traffic_sharing_switch) {
  162. unset($control_item['params']['navList'][$nav_index]);
  163. }
  164. $tab_open = false;
  165. }
  166. }
  167. $control_item['params']['navList'] = array_values($control_item['params']['navList']);
  168. if ($tab_open && $is_store_traffic_sharing_switch) {
  169. array_push($control_item['params']['navList'], [
  170. "pic_url" => 'https://' . \Yii::$app->request->hostName. '/web/v1/statics/clientImg/shop/store.png',
  171. "active_url" => 'https://' . \Yii::$app->request->hostName . '/web/v1/statics/clientImg/shop/storeActive.png',
  172. "nav_text" => "联盟商家",
  173. "tab_type" => "store_sharing",
  174. "link" => [
  175. "name" => "联盟商家",
  176. "link" => "/allianceStore/index/index",
  177. "open_type" => "navigate",
  178. "params" => [],
  179. "id" => "9421"
  180. ]
  181. ]);
  182. }
  183. }
  184. }
  185. $controls = json_encode($controls_arr, JSON_UNESCAPED_UNICODE);
  186. }
  187. return [
  188. 'code' => 0,
  189. 'msg' => '加载成功',
  190. 'data' => [
  191. 'list' => $list,
  192. 'image_search_status' => $imageSearchStatus,
  193. 'business_model' => intval($store->business_model),
  194. 'controls' => $controls
  195. ]
  196. ];
  197. }
  198. }